/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BALoanCell.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 28/08/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BALoanCell.h"
#import "BAPatronLoansViewController.h"
#import "BAUtils.h"
#import <QuartzCore/QuartzCore.h>
#import "BALibrarySettings.h"

@interface BALoanCell ()
@property (weak, nonatomic) IBOutlet UILabel *daysLeftLabel;
@property (weak, nonatomic) IBOutlet UILabel *daysLabel;
@property (weak, nonatomic) IBOutlet UILabel *authorLabel;

@end

@implementation BALoanCell

-(void)awakeFromNib
{
    BA_LABEL_SET_FONT_LIGHT(self.titleLabel);
    BA_LABEL_SET_FONT_LIGHT(self.dateLabel);
    BA_LABEL_SET_FONT_LIGHT(self.renewButton.titleLabel);
    BA_LABEL_SET_FONT_LIGHT(self.daysLeftLabel);
    BA_LABEL_SET_FONT_LIGHT(self.daysLabel);
    BA_LABEL_SET_FONT_LIGHT(self.authorLabel);
}

- (IBAction)renewButtonClicked:(id)sender {
    self.renewButton.hidden=YES;
    [self.loadingIndicator startAnimating];
    [self.parentLoansViewController renewLoan:theLoan];
}

- (void)setLoan:(BALoan *)loan
{
    theLoan=loan;
    
    NSString* titlestring = [NSString stringWithFormat:@"%@%@%@",
                             theLoan.title,
                             ([theLoan.title length]>0 && [theLoan.note length]>0) ? @", " : @"",
                             theLoan.note
                             ];
    
    self.titleLabel.text = titlestring;
    
    NSCalendar *gregorianCalendar = [NSCalendar autoupdatingCurrentCalendar]; // [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);

    NSDate* dueDate = [gregorianCalendar dateFromComponents:[gregorianCalendar components:preservedComponents fromDate:[NSDate dateWithTimeIntervalSince1970:theLoan.loanDueDate]]];
    
    NSDate* currentDate = [gregorianCalendar dateFromComponents:[gregorianCalendar
                                                                 components:preservedComponents
                                                                 fromDate:[NSDate date]]];
    
    
    NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                        fromDate:currentDate
                                                          toDate:dueDate
                                                         options:0];
    if([components day] < 0) {
        self.daysLeftLabel.text = @"00";
        self.daysLeftLabel.textColor = [[BALibrarySettings instance] getCustomerColorNormal]; //was: [UIColor colorWithRed:244.0f/255.0f green:100.0f/255.0f blue:0.0f/255.0f alpha:1.0f];
    } else {
        NSString *paddedStr = [NSString stringWithFormat:@"%02d", [components day]];
        self.daysLeftLabel.text = paddedStr;
        self.daysLeftLabel.textColor = [UIColor whiteColor];
    }

    if(loan.creators.count > 0) {
        self.authorLabel.text = [[NSString stringWithFormat:@"AF %@", [loan.creators objectAtIndex:0]] uppercaseString];
    } else {
        self.authorLabel.text = @"";
    }
    
    NSDate* loandate = [NSDate dateWithTimeIntervalSince1970:theLoan.loanDueDate];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"da_DK"]];
    [dateFormatter setDateFormat:@"d. MMMM yyyy"];
    
    self.dateLabel.text = [[NSString stringWithFormat:@"AFLEVERES: %@", [dateFormatter stringFromDate:loandate]] uppercaseString];
    
    if (theLoan.renewLoanRpcPending) {
        self.renewButton.hidden=YES;
        [self.loadingIndicator startAnimating];
        
    } else {
        self.renewButton.hidden=NO;
        self.renewButton.enabled = theLoan.isLoanRenewable;
        self.renewButton.alpha = theLoan.isLoanRenewable ? 1.0 : 0.25;
        [self.loadingIndicator stopAnimating];
    }
}

@end
